﻿using System;
using System.Collections.Generic;
using System.Text;

namespace oqis.core.common
{
    /// <summary>
    /// 跟时间和日期操作相关的类
    /// </summary>
    public class DateTimeFun
    {
        /// <summary>
        /// 类构建与初始化
        /// </summary>
        public DateTimeFun() { 

        }

        /// <summary>
        /// 获取指定日期所在的周的序号--即获取该日期是一年中的第几周
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public int GetWeekIndexOfYear(DateTime date) { 
            // 判断

            // 获取日期的各部分参数
            int year = date.Year;
            DateTime firstDay = new DateTime(year, 1, 1);

            //当年的第一天是星期几
            int firstOfWeek = Convert.ToInt32(firstDay.DayOfWeek);
            //当年第一周的天数
            int firstWeekDayNum = 7 - firstOfWeek;
            
            //传入日期在当年的天数与第一周天数的差
            int otherDays = date.DayOfYear - firstWeekDayNum;
            //传入日期不在第一周内
            if (otherDays > 0)
            {
                int weekNumOfOtherDays;
                if (otherDays % 7 == 0)
                {
                     weekNumOfOtherDays = otherDays / 7;
                 }
                else
                {
                     weekNumOfOtherDays = otherDays / 7 + 1;
                 }

                return weekNumOfOtherDays + 1;
             }
            //传入日期在第一周内
            else
            {
                return 1;
             }

        }
  
        /// <summary>
        /// 根据某年的第几周获取这周的起止日期
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="weekOrder">第几周</param>
        /// <param name="start">开始日期</param>
        /// <param name="end">结束日期</param>
        public void GetWeekRange(int year,int weekOrder,ref DateTime start, ref DateTime end)
        {
            //非法参数
            if (year <= 0 || weekOrder <= 0 || weekOrder > 53) return;

            //当年的第一天
             DateTime firstDay = new DateTime(year, 1, 1);

            //当年的第一天是星期几
            int firstOfWeek = Convert.ToInt32(firstDay.DayOfWeek);

            //计算当年第一周的起止日期，可能跨年
            int dayDiff = (-1) * firstOfWeek;
            int dayAdd = 6 - firstOfWeek;
             start = firstDay.AddDays(dayDiff).Date;
             end = firstDay.AddDays(dayAdd).Date;
            
            //如果不是要求计算第一周
            if (weekOrder != 1)
            {
                int addDays = (weekOrder -1)*7;
                 start = start.AddDays(addDays);
                 end = end.AddDays(addDays);
             }
         }
   
        /// <summary>
         /// 计算本周的起止日期
        /// </summary>
         /// <param name="start">开始日期</param>
         /// <param name="end">结束日期</param>
        public void GetThisWeekRange(ref DateTime start, ref DateTime end)
        {
            DateTime dt = DateTime.Today;
            int weekNow = Convert.ToInt32(dt.DayOfWeek);
            int dayDiff = (-1) * weekNow;
            int dayAdd = 6 - weekNow;
            start = System.DateTime.Now.AddDays(dayDiff).Date;
            end = System.DateTime.Now.AddDays(dayAdd).Date;
         }
    }
}
